import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)

const app = createApp(App)

// 权限按钮指令
app.directive('permission', {
    mounted(el, binding) {
        const userStore = useUserStore()
        const { value: permission } = binding; // 从指令中获取所需的权限值
        const userPermissions = userStore.menuBntPermission; // 从Vuex获取用户权限

        if (!userPermissions.includes(permission)) {
            el.parentNode && el.parentNode.removeChild(el); // 如果没有权限，则移除元素或隐藏
        }
    }
});
// switch权限指令
app.directive('permissionSwitch', {
    mounted(el, binding) {
        const userStore = useUserStore()
        const { value: permission } = binding; // 从指令中获取所需的权限值
        const userPermissions = userStore.menuBntPermission; // 从Vuex获取用户权限
        if (!userPermissions.includes(permission)) {
            el.textContent = el.getAttribute('data-value') == 'true' ? '已开启':'已禁用'
        }
    }
});
// select权限指令
app.directive('permissionSelect', {
    mounted(el, binding) {
        const userStore = useUserStore()
        const { value: permission } = binding; // 从指令中获取所需的权限值
        const userPermissions = userStore.menuBntPermission; // 从Vuex获取用户权限
        if (!userPermissions.includes(permission)) {
            el.textContent = el.getAttribute('data-value')
        }
    }
});
// list
app.directive('permissionList', {
    mounted(el, binding) {
        const userStore = useUserStore()
        const { value: permission } = binding; // 从指令中获取所需的权限值
        const userPermissions = userStore.menuBntPermission; // 从Vuex获取用户权限
        if (!userPermissions.includes(permission)) {
            el.textContent = '无权限访问列表'
        }
    }
});

// 全局挂载和注册 element-plus 的所有 icon
app.config.globalProperties.$icons = []
// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.config.globalProperties.$icons.push(key)
    app.component(key, component)
}

app.use(router)
app.use(pinia)
app.use(ElementPlus)
app.mount('#app')
